package com.wd.mainmodel.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.Observer;

import com.wd.base.util.Logger;
import com.wd.mainmodel.R;
import com.wd.mainmodel.viewmodel.MainViewModel;

//import androidx.lifecycle.ViewModelProviders;

/**
 * NA<BR>
 *
 * @author w4425
 * @version [V2.0.0, 2021/4/19]
 * @since V2.0.0
 */
public class ContentView extends FrameLayout {
    private static String TAG = "ContentView";
    private MainViewModel mainViewModel;

    public ContentView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public ContentView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ContentView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(@NonNull Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_centent_a, this);
        initViewModel(context);
        observeLivaData(context);
    }

    protected void initViewModel(@NonNull Context context) {
        if (context instanceof FragmentActivity) {
//            mainViewModel = ViewModelProviders.of((FragmentActivity) context).get(MainViewModel.class);
        }
    }

    /**
     * 观察ViewModel的数据
     */
    private void observeLivaData(@NonNull Context context) {
        if (mainViewModel == null) {
            return;
        }
        mainViewModel.getRefreshScoreData().observe((FragmentActivity) context, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                Logger.i("observeLivaData , aBoolean = " + aBoolean);
            }
        });
    }
}
